package cfca.seal.front.service.servlet;

import cfca.sadk.util.Base64;
import cfca.seal.front.service.FunctionType;
import cfca.seal.front.service.ws.IMakeSealService;
import cfca.seal.util.StringUtil;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class MakeSealServlet extends HttpServlet
{
  private static final long serialVersionUID = -2802334706155081655L;
  private static final Logger logger = LoggerFactory.getLogger("service_sys");

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    String type = request.getParameter("type");
    OutputStream os = response.getOutputStream();
    try
    {
      ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      IMakeSealService makeSealService = (IMakeSealService)applicationContext.getBean("makeSealServiceImpl");

      if (type.equals(FunctionType.makeSeal.toString()))
      {
        String pfx = request.getParameter("pfx");
        byte[] pfxFile = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pfx)) {
          pfxFile = Base64.decode(pfx.getBytes("UTF-8"));
        }
        String pfxPassword = request.getParameter("pfxPassword");
        if (StringUtil.isNotEmpty(pfxPassword)) {
          pfxPassword = new String(Base64.decode(pfxPassword.getBytes("UTF-8")), "UTF-8");
        }
        String imageString = request.getParameter("image");
        byte[] image = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(imageString)) {
          image = Base64.decode(imageString.getBytes("UTF-8"));
        }
        String sealInfoXML = request.getParameter("sealInfoXML");
        if (StringUtil.isNotEmpty(sealInfoXML)) {
          sealInfoXML = new String(Base64.decode(sealInfoXML.getBytes("UTF-8")), "UTF-8");
        }
        String makeSeal = makeSealService.makeSeal(pfxFile, pfxPassword, image, sealInfoXML);
        byte[] result = Base64.encode(makeSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.makeNamedSeal.toString()))
      {
        String pfx = request.getParameter("pfx");
        byte[] pfxFile = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pfx)) {
          pfxFile = Base64.decode(pfx.getBytes("UTF-8"));
        }
        String pfxPassword = request.getParameter("pfxPassword");
        if (StringUtil.isNotEmpty(pfxPassword)) {
          pfxPassword = new String(Base64.decode(pfxPassword.getBytes("UTF-8")), "UTF-8");
        }
        String sealInfoXML = request.getParameter("namedSealInfoXML");
        if (StringUtil.isNotEmpty(sealInfoXML)) {
          sealInfoXML = new String(Base64.decode(sealInfoXML.getBytes("UTF-8")), "UTF-8");
        }
        String makeNamedSeal = makeSealService.makeNamedSeal(pfxFile, pfxPassword, sealInfoXML);
        byte[] result = Base64.encode(makeNamedSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.updateSeal.toString()))
      {
        String pfx = request.getParameter("pfx");
        byte[] pfxFile = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pfx)) {
          pfxFile = Base64.decode(pfx.getBytes("UTF-8"));
        }
        String pfxPassword = request.getParameter("pfxPassword");
        if (StringUtil.isNotEmpty(pfxPassword)) {
          pfxPassword = new String(Base64.decode(pfxPassword.getBytes("UTF-8")), "UTF-8");
        }
        String imageString = request.getParameter("image");
        byte[] image = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(imageString)) {
          image = Base64.decode(imageString.getBytes("UTF-8"));
        }
        String sealInfoXML = request.getParameter("sealInfoXML");
        if (StringUtil.isNotEmpty(sealInfoXML)) {
          sealInfoXML = new String(Base64.decode(sealInfoXML.getBytes("UTF-8")), "UTF-8");
        }
        String updateSeal = makeSealService.updateSeal(pfxFile, pfxPassword, image, sealInfoXML);
        byte[] result = Base64.encode(updateSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.updateNamedSeal.toString()))
      {
        String pfx = request.getParameter("pfx");
        byte[] pfxFile = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pfx)) {
          pfxFile = Base64.decode(pfx.getBytes("UTF-8"));
        }
        String pfxPasswd = request.getParameter("pfxPassword");
        if (StringUtil.isNotEmpty(pfxPasswd)) {
          pfxPasswd = new String(Base64.decode(pfxPasswd.getBytes("UTF-8")), "UTF-8");
        }
        String sealInfoXML = request.getParameter("namedSealInfoXML");
        if (StringUtil.isNotEmpty(sealInfoXML)) {
          sealInfoXML = new String(Base64.decode(sealInfoXML.getBytes("UTF-8")), "UTF-8");
        }
        String updateNamedSeal = makeSealService.updateNamedSeal(pfxFile, pfxPasswd, sealInfoXML);
        byte[] result = Base64.encode(updateNamedSeal.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      }

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    catch (Exception e)
    {
      logger.error("C0001", e);
      StringBuilder result = new StringBuilder();
      result.append("<Error><errorCode>");
      result.append("C0001");
      result.append("</errorCode><ErrorMessage>");
      result.append("错误");
      result.append("</ErrorMessage>");

      byte[] returnValue = Base64.encode(result.toString().getBytes("UTF-8"));
      os.write(returnValue, 0, returnValue.length);

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    finally
    {
      if (null != os) {
        os.flush();
        os.close();
      }
    }
  }
}